package com.jpxx.mina;

import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class TCPServerHandler extends IoHandlerAdapter{
	 // 这里我们使用的SLF4J作为日志门面，至于为什么在后面说明。
   // private final static Logger log = LoggerFactory.getLogger(TCPServerHandler.class);

    @Override
    public void messageReceived(IoSession writeFuture , Object message) throws Exception {
        String str = message.toString();
        System.out.println("Server   The message received is [" + str + "]");
        writeFuture.write(str);
        
        //System.out.println(writeFuture.getRemoteAddress());
        //System.out.println(writeFuture.getService());
        if (str.endsWith("quit")) {
        	writeFuture.close(true);
            return;
        }
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("server session created");
        super.sessionCreated(session);
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("server session Opened");
        super.sessionOpened(session);
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("server session Closed");
        super.sessionClosed(session);
    }
}
